import 'package:flutter/material.dart';

import '../../application.dart';

class CaptchaDialog extends StatefulWidget {
  final void Function(String)? onChanged;

  const CaptchaDialog({Key? key, this.onChanged}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _CaptchaDialogState();
}

class _CaptchaDialogState extends State<CaptchaDialog> {
  late Future<String> captcha;

  @override
  initState() {
    super.initState();
    captcha = app.login.verifyImage();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      onChanged: widget.onChanged,
      decoration: InputDecoration(
          hintText: '请填写验证码',
          suffix: FutureBuilder(
            future: captcha,
            builder: (context, imageSrc) {
              if (!imageSrc.hasData) {
                return const SizedBox(
                  width: 30,
                  height: 30,
                  child: CircularProgressIndicator(
                    strokeWidth: 2,
                    color: Colors.black26,
                  ),
                );
              }
              return GestureDetector(
                onTap: () {
                  setState(() {
                    captcha = app.login.verifyImage();
                  });
                },
                child: Image.network(imageSrc.data as String),
              );
            },
          )),
    );
  }
}
